package com.example.extra.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.extra.common.R;
import com.example.extra.entity.UserFile;
import com.example.extra.service.IUserFileService;
import com.example.extra.vo.UserFileVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户文件管理接口
 * @author system
 */
@Slf4j
@Tag(name = "用户文件管理接口")
@RestController
@RequestMapping("/api/userfile")
@AllArgsConstructor
public class UserFileController {
    
    private final IUserFileService userFileService;

    @GetMapping("/list")
    @Operation(summary = "查询用户文件列表")
    @Parameters({
            @Parameter(name = "userId", description = "用户ID"),
            @Parameter(name = "fileName", description = "文件名"),
            @Parameter(name = "category", description = "文件分类"),
            @Parameter(name = "isPublic", description = "是否公开"),
            @Parameter(name = "status", description = "文件状态")
    })
    public R<List<UserFileVo>> getUserFileList(@RequestParam(required = false) Long userId,
                                               @RequestParam(required = false) String fileName,
                                               @RequestParam(required = false) String category,
                                               @RequestParam(required = false) Integer isPublic,
                                               @RequestParam(required = false) Integer status) {
        try {
            QueryWrapper<UserFile> queryWrapper = new QueryWrapper<>();
            
            if (userId != null) {
                queryWrapper.eq("user_id", userId);
            }
            if (fileName != null && !fileName.trim().isEmpty()) {
                queryWrapper.like("file_name", fileName);
            }
            if (category != null && !category.trim().isEmpty()) {
                queryWrapper.eq("category", category);
            }
            if (isPublic != null) {
                queryWrapper.eq("is_public", isPublic);
            }
            if (status != null) {
                queryWrapper.eq("status", status);
            }
            
            List<UserFile> userFileList = userFileService.list(queryWrapper);
            List<UserFileVo> userFileVoList = userFileList.stream()
                    .map(userFile -> {
                        UserFileVo userFileVo = new UserFileVo();
                        BeanUtils.copyProperties(userFile, userFileVo);
                        return userFileVo;
                    })
                    .collect(Collectors.toList());
            
            return R.ok("用户文件列表查询成功!", userFileVoList);
        } catch (Exception e) {
            log.error("查询用户文件列表失败", e);
            return R.fail("查询用户文件列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/getById")
    @Operation(summary = "根据ID查询用户文件")
    @Parameter(name = "id", description = "用户文件ID", required = true)
    public R<UserFileVo> getUserFileById(@RequestParam Long id) {
        try {
            UserFile userFile = userFileService.getById(id);
            if (userFile == null) {
                return R.fail("用户文件不存在!");
            }
            
            UserFileVo userFileVo = new UserFileVo();
            BeanUtils.copyProperties(userFile, userFileVo);
            
            return R.ok("用户文件查询成功!", userFileVo);
        } catch (Exception e) {
            log.error("根据ID查询用户文件失败", e);
            return R.fail("查询用户文件失败: " + e.getMessage());
        }
    }

    @GetMapping("/getByUserId")
    @Operation(summary = "根据用户ID查询文件列表")
    @Parameter(name = "userId", description = "用户ID", required = true)
    public R<List<UserFileVo>> getUserFileByUserId(@RequestParam Long userId) {
        try {
            QueryWrapper<UserFile> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            
            List<UserFile> userFileList = userFileService.list(queryWrapper);
            List<UserFileVo> userFileVoList = userFileList.stream()
                    .map(userFile -> {
                        UserFileVo userFileVo = new UserFileVo();
                        BeanUtils.copyProperties(userFile, userFileVo);
                        return userFileVo;
                    })
                    .collect(Collectors.toList());
            
            return R.ok("根据用户ID查询文件成功!", userFileVoList);
        } catch (Exception e) {
            log.error("根据用户ID查询文件失败", e);
            return R.fail("查询文件失败: " + e.getMessage());
        }
    }

    @GetMapping("/getByFileId")
    @Operation(summary = "根据文件ID查询用户文件关联")
    @Parameter(name = "fileId", description = "文件ID", required = true)
    public R<List<UserFileVo>> getUserFileByFileId(@RequestParam Long fileId) {
        try {
            QueryWrapper<UserFile> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("file_id", fileId);
            
            List<UserFile> userFileList = userFileService.list(queryWrapper);
            List<UserFileVo> userFileVoList = userFileList.stream()
                    .map(userFile -> {
                        UserFileVo userFileVo = new UserFileVo();
                        BeanUtils.copyProperties(userFile, userFileVo);
                        return userFileVo;
                    })
                    .collect(Collectors.toList());
            
            return R.ok("根据文件ID查询用户文件关联成功!", userFileVoList);
        } catch (Exception e) {
            log.error("根据文件ID查询用户文件关联失败", e);
            return R.fail("查询用户文件关联失败: " + e.getMessage());
        }
    }

    @GetMapping("/getPublicFiles")
    @Operation(summary = "查询公开文件列表")
    @Parameters({
            @Parameter(name = "category", description = "文件分类"),
            @Parameter(name = "fileName", description = "文件名")
    })
    public R<List<UserFileVo>> getPublicFiles(@RequestParam(required = false) String category,
                                              @RequestParam(required = false) String fileName) {
        try {
            QueryWrapper<UserFile> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("is_public", 1).eq("status", 0);
            
            if (category != null && !category.trim().isEmpty()) {
                queryWrapper.eq("category", category);
            }
            if (fileName != null && !fileName.trim().isEmpty()) {
                queryWrapper.like("file_name", fileName);
            }
            
            List<UserFile> userFileList = userFileService.list(queryWrapper);
            List<UserFileVo> userFileVoList = userFileList.stream()
                    .map(userFile -> {
                        UserFileVo userFileVo = new UserFileVo();
                        BeanUtils.copyProperties(userFile, userFileVo);
                        return userFileVo;
                    })
                    .collect(Collectors.toList());
            
            return R.ok("公开文件列表查询成功!", userFileVoList);
        } catch (Exception e) {
            log.error("查询公开文件列表失败", e);
            return R.fail("查询公开文件列表失败: " + e.getMessage());
        }
    }
}
